package com.webserver_v5.http;

import com.sun.xml.internal.ws.server.ServerRtException;

import javax.activation.MimetypesFileTypeMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HttpServletResponse {
    private Socket socket;
    private int statusCode = 200;
    private String statusReason = "OK";

    private File contentFile;

    private Map<String,String>headers = new HashMap<>();

    private MimetypesFileTypeMap mime = new MimetypesFileTypeMap();
    public HttpServletResponse(Socket socket){
        this.socket = socket;

    }
    public void response() throws IOException {
        //发送状态行
        sendStatusLine();
        //发送响应头
        sendHeader();
        //发送响应正文
        sendContent();

    }
    //发送状态行
    private void sendStatusLine() throws IOException {
        println("HTTP/1.1 "+statusCode+" "+statusReason);
    }

    //发送响应头
    private void sendHeader() throws IOException {
        Set<Map.Entry<String ,String >> entrySet = headers.entrySet();
        for (Map.Entry<String,String>e : entrySet){
            String key = e.getKey();
            String value = e.getValue();
            println(key+": "+value);
        }
        //单独发送回车和换行表示响应头发送完毕
        println("");
    }

    //发送响应正文
    private void sendContent() throws IOException {
        if (contentFile != null) {
            FileInputStream fis = new FileInputStream(contentFile);
            OutputStream out = socket.getOutputStream();

            byte[] data = new byte[10240];
            int len;
            while ((len = fis.read(data)) != -1) {
                out.write(data, 0, len);
            }
        }
    }
    private void println(String line) throws IOException {
        OutputStream out = socket.getOutputStream();
        out.write(line.getBytes(StandardCharsets.ISO_8859_1));
        out.write(13);
        out.write(10);
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getContentFile() {
        return contentFile;
    }

    public void setContentFile(File contentFile) {
        this.contentFile = contentFile;
        addHeader("Content-Type",mime.getContentType(contentFile));
        addHeader("Content-Length",contentFile.length()+"");
    }

    public void addHeader(String name,String value){
        headers.put(name,value);
    }


    public void sendRedirect(String location){
        /*
            重定向的响应要求:
            1:状态代码为302
            2:包含一个响应头Location，用于指定要求浏览器重定向的路径
         */
        statusCode = 302;
        statusReason = "Moved Temporarily";
        addHeader("Location",location);

    }
}
